package com.test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.jdbc.core.JdbcTemplate;

import com.test.model.TableBean;

public class DbHelp {

	private String url;
	private String username;
	private String password;
	private String driverClassName;
	
	
	public String getDriverClassName() {
		return driverClassName;
	}
	public void setDriverClassName(String driverClassName) {
		this.driverClassName = driverClassName;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public DbHelp(String url, String username, String password,
			String driverClassName) {
		this.url = url;
		this.username = username;
		this.password = password;
		this.driverClassName = driverClassName;
	}
	public DataSource initDataSource(){
		BasicDataSource b =new BasicDataSource();
		b.setDriverClassName(driverClassName);
		b.setUrl(url);
		b.setUsername(username);
		b.setPassword(password);
		return b;
	}
	
	public JdbcTemplate  getTenmplate(){
		
		return  new JdbcTemplate(initDataSource());
	}
	
	public static DbHelp getDbHelp(){
		return new DbHelp("jdbc:mysql://10.4.14.186:3306/d94d43107a04d49f3937366b3ccc1cfdd","uH76k7W657U0E","pNj6hHm2xsjQp","com.mysql.jdbc.Driver");
				
			/*("jdbc:mysql://localhost:3306/nutch","root","root","com.mysql.jdbc.Driver")*/
					/*("jdbc:mysql://192.168.1.241:3306/edudb","eduadmin","1qaz2wsx","com.mysql.jdbc.Driver");*/
	}
	
	public List<TableBean> getTables(){
		DataSource datasource = initDataSource();
		try {
			Connection conn = datasource.getConnection();
			//获取所有表名
			ResultSet rs = conn.getMetaData().getTables(null, null, null, new String[]{"TABLE"});
				List<String> tableName=new ArrayList<String>();
				while(rs.next()){
					/* System.out.print("目录名："+rs.getString(1));
				      System.out.print(" 模式名："+rs.getString(2));
				      System.out.print(" 表名："+rs.getString(3));
				      System.out.print(" 表的类型："+rs.getString(4));
				      System.out.println(" 注释："+rs.getString(5));*/
				      tableName.add(rs.getString(3));
			 	} 
			
			 	List<TableBean> tabbeanList=new ArrayList<TableBean>();
			 //通过表名获取列名,此处有冗余实际开发中请	
			for(String name:tableName){
				TableBean tab=new TableBean(name);
				List<String> col=new ArrayList<String>();
				ResultSet res = conn.getMetaData().getColumns(null, null,name, null);
				while(res.next()){
					col.add(res.getString(4));
					/*System.out.println("表名      "+res.getString(3));		 
					System.out.println("列名      "+res.getString(4));	*/
					//System.out.println("类型名      "+res.getString(5));	
				}
				tab.setColumn(col);
				tabbeanList.add(tab);
			}
			
			conn.close();
			/*for(TableBean b:tabbeanList){
				System.out.println(b);
			}*/
			return tabbeanList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	
	
	
	
	public static void main(String[] args) {
		DbHelp dbhelp= new DbHelp("jdbc:mysql://localhost:3306/nutch","root","root","com.mysql.jdbc.Driver");
		JdbcTemplate template=dbhelp.getTenmplate();
		int yaer=1995;
		for(int i=0;i<10;i++){
			  yaer=yaer+1;
 			template.execute("INSERT into `line` (value,name) VALUE("+32+",'并发数量')");
		}
		
		
	 	/*List<Map<String, Object>> list = template.queryForList("SELECT name,num as value FROM test");
			System.out.println(list.size());
			
			List<PieBean> listPie=new ArrayList<PieBean>();
			for(Map<String, Object> m:list){
				 PieBean pb=new PieBean();
				 try {
					BeanUtils.populate(pb, m);
					 listPie.add(pb);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			}
			for(PieBean b:listPie){
				System.out.println(b);
			}*/
	
	}
}
